package com.zhs.smartmall.ware.controller;



import com.zhs.smartmall.ware.service.IWmsWareInfoService;
import com.zhs.smartmall.ware.model.WmsWareInfo;
import com.zhs.smartmall.ware.dto.WmsWareInfoDto;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhs.smartmallcommon.Enum.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.zhs.smartmallcommon.exception.ServiceException;
import org.springframework.transaction.annotation.Transactional;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author 
 * @since 2023-11-15
 */
@RestController
@RequestMapping("wms/ware/info")
@Api(value = "")
@Slf4j
public class WmsWareInfoController {

    @Resource
    private IWmsWareInfoService  iWmsWareInfoService;

    @ApiOperation(value = "分页查询")
    @GetMapping("/getByPage")
    public R<IPage<WmsWareInfo>> getListByPage(
        @RequestParam(value = "currentPage", required = false, defaultValue = "1")Integer currentPage,
        @RequestParam(value = "pageSize", required = false, defaultValue = "10") Integer pageSize){
        //构建分页
        Page<WmsWareInfo> page = new Page<>(currentPage, pageSize);
        LambdaQueryWrapper<WmsWareInfo> lambda = new QueryWrapper<WmsWareInfo>().lambda();
        //此处可以拼条件
        lambda.eq(WmsWareInfo::getIsDeleted,0);
        IPage<WmsWareInfo> pages =  iWmsWareInfoService.page(page, lambda);
        return R.ok(pages);
    }


    @ApiOperation(value = "通过id查询")
    @GetMapping("/getById")
    public R<WmsWareInfo> getById(@RequestParam("id") String id){
        if (StringUtils.isBlank(id)) throw new ServiceException("id不能为空");
        LambdaQueryWrapper<WmsWareInfo> wrapper = new QueryWrapper<WmsWareInfo>()
                            .lambda().eq(WmsWareInfo::getId,id).eq(WmsWareInfo::getIsDeleted,0);
        return R.ok(iWmsWareInfoService.getOne(wrapper));
    }

    @ApiOperation(value = "通过id删除数据")
    @GetMapping("/delete/{id}")
    @Transactional
    public R deleteById(@PathVariable("id") String id){
        if (StringUtils.isBlank(id)) throw new ServiceException("id不能为空");
        //逻辑删除
        LambdaQueryWrapper<WmsWareInfo> query = new QueryWrapper<WmsWareInfo>().lambda().eq(WmsWareInfo::getId, id).eq(WmsWareInfo::getIsDeleted, 0);
        WmsWareInfo wmsWareInfo = iWmsWareInfoService.getOne(query);
        if(ObjectUtils.isEmpty(wmsWareInfo)) throw new ServiceException("该数据不存在或者已经被删除");
        wmsWareInfo.setIsDeleted(1);
        iWmsWareInfoService.updateById(wmsWareInfo);
        return R.ok("数据删除成功");
    }


    @ApiOperation(value = "批量删除数据")
    @PostMapping("/deleteByIds")
    @Transactional
    public R deleteByIds(@RequestBody WmsWareInfoDto wmsWareInfoDto){
        if (CollectionUtils.isEmpty(wmsWareInfoDto.getIdList())) throw new ServiceException("要删除的id不能为空!");
        //逻辑删除
        List<WmsWareInfo> list = new ArrayList<>();
        wmsWareInfoDto.getIdList().stream().forEach(id ->{
            WmsWareInfo wmsWareInfo = iWmsWareInfoService.getById(id);
            if (ObjectUtils.isEmpty(wmsWareInfo)) throw new ServiceException("id为" + id + "的数据不存在");
            if (1 == wmsWareInfo.getIsDeleted()) throw new ServiceException("id为" + id + "的数据已经被删除");
            wmsWareInfo.setIsDeleted(1);
            list.add(wmsWareInfo);
        });
        iWmsWareInfoService.updateBatchById(list);
        return R.ok("数据删除成功");
    }

    @ApiOperation(value = "新增或者更新数据")
    @PostMapping("/addOrUpdate")
    public R addOrUpdate(@RequestBody WmsWareInfoDto wmsWareInfoDto){
        return iWmsWareInfoService.addOrUpdate(wmsWareInfoDto);
    }

}
